import random,logging
from django.shortcuts import render
from django.views import View
from django_redis import get_redis_connection
from django.http import HttpResponse,HttpResponseForbidden
from django import http


from . import constants
from meiduo_mall.utils.response_code import RETCODE
from verifications.libs.captcha.captcha import captcha
from verifications.libs.yuntongxun.ccp_sms import CCP
from celery_tasks.sms.tasks import ccp_send_sms_code


# Create your views here.


class ImageCodeView(View):
    def get(self,request,uuid):
        pass
        text,image = captcha.generate_captcha()
        # 保存图形验证码
        redis_conn = get_redis_connection('verify_code')
        redis_conn.setex('img_%s' % uuid, constants.IMAGE_CODE_REDIS_EXPIRES, text)

        # 响应图形验证码
        return http.HttpResponse(image, content_type='image/jpg')


class SMSCodeView(View):
    """
    短信验证码
    """
    def get(self,request,mobile):
        """

        :param mobile: 手机号
        :return: json
        """
        # 创建连接到redis的对象
        redis_conn = get_redis_connection('verify_code')

        image_code_client = request.GET.get('image_code')
        uuid = request.GET.get('uuid')
        send_flag = redis_conn.get('send_flag_%s' % mobile)
        if send_flag:
            return http.JsonResponse({'code': RETCODE.THROTTLINGERR, 'errmsg': '发送短信过于频繁'})

        # 检验参数是否齐全
        if not all([image_code_client,uuid]):
            return http.JsonResponse({'code': RETCODE.IMAGECODEERR,'errmsg': '缺少必传参数'})


        # 提取图形验证码
        image_code_server = redis_conn.get('img_%s' % uuid)
        # 检测图形验证码是否过期
        if image_code_client is None:
            return http.JsonResponse({'code': RETCODE.IMAGECODEERR,'errmsg': '图形验证码已失效'})

        # 删除图形验证码，防止恶意测试
        redis_conn.delete('img_%s' % uuid)
        # 对比图形验证码
        image_code_server = image_code_server.decode()  # bytes转字符串
        if image_code_client.lower() != image_code_server.lower():  # 转小写后比较
            return http.JsonResponse({'code': RETCODE.IMAGECODEERR, 'errmsg': '输入图形验证码有误'})

        # 生成短信验证码：生成6位数验证码
        sms_code = '%06d' % random.randint(0,999999)
        print(sms_code)
        logger = logging.getLogger('django')
        logger.info(sms_code)

        # 创建Redis管道
        pl = redis_conn.pipeline()
        # 将Redis请求添加到队列
        pl.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
        pl.setex('send_flag_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, 1)
        # 执行请求
        pl.execute()
        # Celery异步发送短信验证码
        ccp_send_sms_code.delay(mobile, sms_code)


        # # 保存短信验证码
        # redis_conn.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
        # # 重新写入send_flag
        # redis_conn.setex('send_flag_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, 1)
        # # 发送短信验证码
        # CCP().send_template_sms(mobile,[sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60], constants.SEND_SMS_TEMPLATE_ID)

        # 响应结果
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '发送短信成功'})




